package com.jingyuan.biz.manager;

import com.google.common.collect.Lists;
import io.netty.channel.Channel;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 远程主机节点
 */
@Getter
@Setter
@ToString
public class JNode {
    private volatile Boolean isMaster=false;   //是否是Master节点
    private volatile long lastKeepLiveTime;  //上次保活时间
    private List<JNode> childNodes;     //孩子节点
    private int voteValue;  //选举时此节点产生的随机值，随机值最小的节点为master节点
    private String msg;
    private String address; //远程主机的地址
    private int port;       //远程主机端口号
    private Channel channel;    //与此node所代表的主机通信的通道

    public JNode(){
//        List<JNode> jNodeList= Lists.newArrayList();
//        childNodes=Collections.synchronizedList(jNodeList); //同步包装
//        //childNodes =jNodeList;
        childNodes=new CopyOnWriteArrayList<>();
    }

    public List<JNode> getChildNodes() {
        return childNodes;
    }

    public void addChildNode(JNode node){
        childNodes.add(node);
    }

    public void removeChildNode(JNode node){
        childNodes.remove(node);
    }

    public void removeChildNode(int index){
        childNodes.remove(index);
    }
}
